package com.hnichr.ump.assessment.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.beisen.sdk.Constants;
import com.beisen.sdk.assessment.Assessment;
import com.beisen.sdk.assessment.model.*;
import com.hnichr.ump.common.assessment.service.AssessmentInterfService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AssessmentInterfServiceImpl implements AssessmentInterfService {

    private static final Logger logger = LoggerFactory.getLogger(AssessmentInterfServiceImpl.class);

    @Value("${assessment.enabled}")
    private boolean assessmentEnabled;

    private static final AccountToken ACCOUNT_TOKEN;

    private static final Assessment ASSESSMENT;

    static {
        ACCOUNT_TOKEN = new AccountToken(com.beisen.sdk.Constants.TENANT_ID,
                com.beisen.sdk.Constants.TOKEN,com.beisen.sdk.Constants.SECRET, Constants.BEISEN_ACCOUNT);
        ASSESSMENT = new Assessment();
    }

    /**
     * 获取活动列表
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @Override
    public ActivityInfoList getActivityList(Integer pageSize, Integer pageIndex){
        try{
            return ASSESSMENT.getActivityList(ACCOUNT_TOKEN,pageSize,pageIndex);
        }
        catch (Exception e){
            logger.error("获取活动列表异常",e);
            return null;
        }
    }
    /**
     * 获取邮件模板
     * @return
     */
    @Override
    public TemplateList getEmailTemplateList(){
        try{
            return ASSESSMENT.getEmailTemplateList(ACCOUNT_TOKEN);
        }
        catch (Exception e){
            logger.error("获取邮件模板异常",e);
            throw new RuntimeException(e);
        }
    }
    /**
     * 发送邮件邀请受测者
     * @return
     */
    @Override
    public TesteeResult sendEmailInviteTestee(Integer activityId, String startTime,
                                              String endTime, String emailTemplate, TesteeInfoParameterList testeeList){
        try{
            if(assessmentEnabled){
                return ASSESSMENT.sendEmailInviteTestee(ACCOUNT_TOKEN,activityId,startTime,endTime,emailTemplate,testeeList);
            }
            //模拟值
            else{
                TesteeResult testeeResult = new TesteeResult();
                testeeResult.testee_invite_result_list = new ArrayList<>();
                for(TesteeInfoParameter parameter:testeeList){
                    TesteeInviteResult r = new TesteeInviteResult();
                    r.activity_id = activityId;
                    r.name = parameter.name;
                    r.email = parameter.email;

                    r.sn = RandomUtil.randomNumbers(13);
                    r.login_url = "https://hnikrchr.ceping.com/";
                    r.test_url = "https://m.baidu.com/";
                    r.start_time = startTime;
                    r.end_time = endTime;
                    testeeResult.testee_invite_result_list.add(r);
                }
                return testeeResult;
            }
        }
        catch (Exception e){
            logger.error("发送邮件邀请受测者异常",e);
            return null;
        }
    }

    /**
     * 获取活动下受测者答题状态
     * @param activityId
     * @param emails
     * @return
     */
    @Override
    public ActivityTesteeState getActivityTesteeState(Integer activityId, String emails){
        try{
            return ASSESSMENT.getActivityTesteeState(ACCOUNT_TOKEN,activityId,emails);
        }
        catch (Exception e){
            logger.error("获取受测者答题状态异常",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过通行证号和邮箱获取租户下受测者作答结果
     * @param activityId
     * @param emailSnList
     * @return
     */
    @Override
    public ActivityTesteeResultList getTesteeResultByEmailAndSN(Integer activityId, List<EmailSNParameter> emailSnList){
        try{
            return ASSESSMENT.getTesteeResultByEmailAndSN(ACCOUNT_TOKEN,activityId,emailSnList);
        }
        catch (Exception e){
            logger.error("通过通行证号和邮箱获取租户下受测者作答结果异常",e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getReportDownloadUrl(String reportNo){
        try{
            String url =  ASSESSMENT.getReportDownloadUrl(ACCOUNT_TOKEN,reportNo);
            //返回的字符串被双引号包围，要去掉第一个和最后一个字符
            return StringUtils.substring(url,1,url.length()-1);
        }
        catch (Exception e){
            logger.error("通过报告号获取报告下载链接异常",e);
            return null;
        }
    }






}
